import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class CajasSorpresa here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class CajaSorpresa extends Bonus
{
    /**
     * Act - do whatever the CajasSorpresa wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    private GreenfootImage caja1=new GreenfootImage("Sorpresa1.png");
    private GreenfootImage caja2=new GreenfootImage("Sorpresa2.png");
    private GreenfootImage caja3=new GreenfootImage("Sorpresa3.png");
    private GreenfootImage regalo1=new GreenfootImage("Tanque.png");
    private GreenfootImage regalo2=new GreenfootImage("10Monedas.png");
    private GreenfootImage regalo3=new GreenfootImage("Nave.png");

    private int animacion;
    private int Caja;
    private int provavilidad;
    private int regalo;
    private int desapareseCaja;

    public void act() 
    {
        // Add your action code here.}
        if(Caja==0){  
            animacion();
            Actor stickman= getOneIntersectingObject(Stickman.class);    
            if(stickman!=null){ 
                getWorld().addObject(new CajaSorpresa(1),556,17);                                                       
                getWorld().removeObject(this);
                return;
            }
            move(2);
        }
        if(Caja==1){
            if(provavilidad>=0){
                provavilidad--;animacion2();
            }else{
                desapareseCaja--;
                if(desapareseCaja<=0){
                    World aux=getWorld();
                    ((StickmanWorld)aux).creaNuevasBarrasDeEnergia(1);
                    
                    getWorld().removeObject(this);}

            }
        }    

        
    }   

    public void animacion(){
        animacion++;
        if(animacion==20){setImage(caja2);}
        if(animacion==40){setImage(caja3);}
        if(animacion==60){animacion=0;setImage(caja1);}
    }

    public CajaSorpresa(int abreCaja){
        if(abreCaja==0){setImage(caja1);}
        if(abreCaja==1){setImage(regalo1);}
        animacion=0;
        Caja=abreCaja;
        provavilidad=Greenfoot.getRandomNumber(100)+200;
        regalo=0;
        desapareseCaja=100;
    }

    public void animacion2(){
        animacion++;
        if(animacion==20){regalo=1;setImage(regalo2);}
        if(animacion==40){regalo=2;setImage(regalo3);}
        if(animacion==60){regalo=3;animacion=0;setImage(regalo1);}
    }
}
